package com.crm.system.service.impl.memberManager;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.crm.model.dto.InsertMemberShipCardCallable;
import com.crm.model.vo.memberManager.MemberShipCardImportVO;

import com.crm.service.memberManager.MemberCardDbService;
import com.crm.service.sys.ImportExportDbService;
import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import com.crm.system.service.memberManager.MemberShipCardImportService;
import com.crm.system.service.sys.ImportExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author luojie
 * @date 2021/6/7 14:54
 */
@Service
public class MemberShipCardImportServiceImpl implements MemberShipCardImportService {

    @Autowired
    private SysCompanyDbService companyDbService;
    @Autowired
    private MemberCardDbService memberCardDbService;
    @Autowired
    private ImportExportService importExportService;

    @Override
    public JSONObject memberShipCardImport(List<MemberShipCardImportVO> list) {
        //返回的结果集
        JSONObject jsonObject = new JSONObject();

        int count = 100;                   //一个线程处理300条数据
        int listSize = list.size();        //数据集合大小
        //int runSize = (listSize/count)+1;  //开启的线程数
        int runSize = importExportService.calculateImportedThreads(listSize);
        List<MemberShipCardImportVO> newlist = null;       //存放每个线程的执行数据

        ExecutorService executor = Executors.newCachedThreadPool();
        //创建两个个计数器
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(runSize);
        List<FutureTask<JSONObject>> taskList = new ArrayList<FutureTask<JSONObject>>();
        for (int i = 0; i < runSize ; i++) {
            //计算每个线程执行的数据
            if ((i + 1) == runSize) {
                int startIndex = (i * count);
                int endIndex = list.size();
                newlist = list.subList(startIndex, endIndex);
            } else {
                int startIndex = (i * count);
                int endIndex = (i + 1) * count;
                newlist = list.subList(startIndex, endIndex);
            }
            FutureTask<JSONObject> futureTask = new FutureTask(new InsertMemberShipCardCallable(newlist, begin, end,companyDbService,memberCardDbService));
            taskList.add(futureTask);
            executor.submit(futureTask);//submit返回一个Future，代表了即将要返回的结果
        }

        //导入数量
        int importNum = 0;
        //会员卡卡号为空
        List<Integer> membershipCardEmptyIdList = new ArrayList();
        //会员卡卡号重复
        List<Integer> repeatIdList = new ArrayList();
        //导入失败
        List<Integer> importFailIdList = new ArrayList();

        for (int i = 0; i < taskList.size(); i++) {
            try {
                // FutureTask的get方法会自动阻塞,直到获取计算结果为止
                //导入数量
                importNum = importNum + taskList.get(i).get().getInteger("importNum");

                //会员卡卡号为空
                JSONArray membershipCardEmptyIdArray = taskList.get(i).get().getJSONArray("membershipCardEmptyIdList");
                for (int j = 0; j < membershipCardEmptyIdArray.size(); j++){
                    int  membershipCardEmpty = Integer.parseInt(membershipCardEmptyIdArray.get(j).toString());
                    membershipCardEmptyIdList.add(membershipCardEmpty + i*100);
                }

                //会员卡卡号重复
                JSONArray repeatIdArray = taskList.get(i).get().getJSONArray("repeatIdList");
                for (int j = 0; j < repeatIdArray.size(); j++){
                    int repeat = Integer.parseInt(repeatIdArray.get(j).toString());
                    repeatIdList.add(repeat + i*100);
                }

                //导入失败
                JSONArray importFailIdArray = taskList.get(i).get().getJSONArray("importFailIdList");
                for (int j = 0; j < importFailIdArray.size(); j++){
                    int importFail = Integer.parseInt(importFailIdArray.get(j).toString());
                    importFailIdList.add(importFail + i*100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        // 关闭线程池
        executor.shutdown();

        jsonObject.put("importNum", importNum);
        jsonObject.put("repeatIdList", repeatIdList);
        jsonObject.put("membershipCardEmptyIdList", membershipCardEmptyIdList);
        jsonObject.put("importFailIdList", importFailIdList);

        //插入导入表
        if (importNum > 0) {
            importExportService.insertExportService(importNum,"1","1","5");
        }
        return jsonObject;
    }
}
